home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Starfield1.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  3.1 KB  |  83 lines

  1. ' Star field demo 1
  2. '
  3. ' This is also a bit of a tutorial on OpenGL and how Basic4GL does things.
  4.  
  5. const maxStars = 200
  6.  
  7. dim stars#(maxStars)(2)
  8. ' Note: An array of maxStars 3D vectors.
  9. ' The # after it means floating point. Otherwise it would store integers (the default in Basic4GL)
  10.  
  11. dim i               ' Note: ALL variables must be declared before use, with DIM
  12.  
  13. ' Populate star field
  14. for i = 1 to maxStars
  15.     stars#(i) = vec3 (rnd () % 201 - 100, rnd () % 201 - 100, -i)
  16.     ' Vec3 creates a 3D vector. Parameters are X, Y and Z
  17.     ' Rnd() returns a number between 0 and MAXINT (MAXINT = about 2 billion),
  18.     ' So we use the % (mod) operator to convert it to a number between 0 and 200.
  19.     
  20.     ' We have to setup our vectors with respect to the coordinate system.
  21.     ' In OpenGL, the coordinate system says that:
  22.     '   X = How many units to the RIGHT.
  23.     '   Y = How many units UP.
  24.     '   Z = How many units OUT OF THE SCREEN.
  25.     ' 
  26.     ' So because we want our stars to appear INTO the screen, we have to use a 
  27.     ' NEGATIVE Z value.
  28. next
  29.  
  30. glDisable (GL_DEPTH_TEST)
  31. ' This disables the Z buffer. 
  32. ' (The Z buffer automatically determines which pixels are infront of the other ones.
  33. '  We don't need it for a star field though.)
  34.  
  35. ' Main loop
  36. while true    
  37.     glClear (GL_COLOR_BUFFER_BIT)       
  38.     ' This clears the screen.
  39.     ' The "colour buffer" refers to the actual image that we are going to draw.
  40.     ' There are other buffers (like the Z buffer) that we could clear also if we
  41.     ' were using them. But we're not :)
  42.  
  43.     for i = 1 to maxStars
  44.     
  45.         ' Move the star forward, by adding 1 to Z
  46.         stars#(i) = stars#(i) + vec3 (0, 0, 1)
  47.  
  48.         ' If the Z goes positive (infront of the screen), move it to the back again.
  49.         if stars#(i)(2) >= 0 then stars#(i)(2) = -maxStars endif
  50.         ' Note: All "if" statements must have an "endif", even if they are 
  51.         ' only on 1 line.
  52.         
  53.         ' Draw the star.
  54.         ' In OpenGL you don't actually access the pixels directly.
  55.         ' Instead you pass 3D data into OpenGL. It will do the 3D maths for you
  56.         ' and draw the appropriate image on the screen.
  57.         
  58.         ' You place the 3D data between a glBegin() and a glEnd().
  59.         ' We want it to plot points (each point is a star), so we pass GL_POINTS
  60.         ' to glBegin ()
  61.         glBegin (GL_POINTS)
  62.             glVertex3fv (stars#(i))
  63.             ' This is a form of the glVertex command.
  64.             ' The last 3 characters give us information about this particular
  65.             ' version.
  66.             ' 3 = Takes a 3D vector
  67.             ' F = Floating point values
  68.             ' V = Vector is passed in as an array
  69.         glEnd ()
  70.     next
  71.     
  72.     SwapBuffers ()
  73.     ' Basic4GL uses "double buffered" mode.
  74.     ' All drawing happens in the off-screen buffer.
  75.     ' When we're finished, we "swap" it to the onscreen buffer by calling SwapBuffers()
  76.  
  77.     WaitTimer (20)
  78.     ' Wait timer slows us down. Parameter is the interval.
  79.     ' Note: Frames per second (FPS) = 1000 / Interval
  80.     '
  81.     ' So 1000 / 20 = 50 frames per second
  82. wend
  83.